﻿function Widget_TaskList(obj)
{
   this.initializeBase(obj);
}
Widget_TaskList.prototype = new WidgetInstance;

Widget_TaskList.prototype.Load = function()
{
   this.Widget.InstanceLoad();
   //runs any script element inside the div
   execJS(this.WidgetDiv);   
   this.Widget.InstanceLoadComplete();
   
   this.TextBox_Add = $get('txt_ToDo_Add' + this.id);
   this.Tasks_Table = $get('tbl_Tasks' + this.id);
   for(var i=0;i<this.Tasks_Table.childNodes.length;i++)
   {
      if(this.Tasks_Table.childNodes[i].tagName && this.Tasks_Table.childNodes[i].tagName.toUpperCase() == "TBODY")
      {
         this.Tasks_TBody = this.Tasks_Table.childNodes[i];
         break;
      }
   }
   
   this.LastID=1;
   this.Items = new Array();
}

Widget_TaskList.prototype.AddTask = function()
{
   this.AddTaskItem(this.TextBox_Add.value);
}

Widget_TaskList.prototype.DeleteTask = function(id)
{
   for(var i=0;i<this.Items.length;i++)
   {
      var theItem = this.Items[i];
      if (theItem.id == id)
      {
         theItem.InPlaceEditor.dispose();
         theItem.InPlaceCollectionEditor.dispose();
         this.Tasks_TBody.removeChild(theItem.TR);
         if (i==0)
         {
            this.Items = this.Items.slice(1);
         }
         else if (i == (this.Items.length - 1))
         {
            this.Items = this.Items.slice(0, this.Items.length -1);
         }
         else
         {
            var before = this.Items.slice(0, i);
            var after = this.Items.slice(i+1);
            this.Items = before.concat(after);
         }
         break;
      }
   }
}

Widget_TaskList.prototype.AddTaskItem = function(text)
{
   if (text.length > 0)
   {
      var tr = document.createElement("tr");
      tr.setAttribute("valign", "top");
      tr.id = "tr_todo_" + this.id + "_" + this.LastID;
      
      var td_Priority = document.createElement("td");      
      var div_Priority = document.createElement("div");
      div_Priority.id = "td_todo_priority_" + this.id + "_" + this.LastID;
      div_Priority.appendChild(document.createTextNode("low"));
      td_Priority.appendChild(div_Priority);

      var td_Task = document.createElement("td");
      var div_Task = document.createElement("div");
      div_Task.id = "td_todo_task_" + this.id + "_" + this.LastID;
      div_Task.appendChild(document.createTextNode(text));
      td_Task.appendChild(div_Task);

      var td_Actions = document.createElement("td");
      td_Actions.innerHTML = "<a onclick=\"$WI('" + this.id + "').DeleteTask(" + this.LastID + ")\" title='Delete Task'>[x]</a>";
      
      td_Priority.style.width="5%";
      td_Task.style.width="90%";
      td_Actions.style.width="5%"
      tr.appendChild(td_Priority);
      tr.appendChild(td_Task);
      tr.appendChild(td_Actions);
      this.Tasks_TBody.appendChild(tr);
      
      var TaskItem = {}
      TaskItem.id = this.LastID++;
      TaskItem.TR = tr;
      TaskItem.TD_Priority = td_Priority;
      TaskItem.TD_Task = td_Task;
      TaskItem.TD_Actions = td_Actions;
      TaskItem.InPlaceEditor = new Ajax.InPlaceEditor(div_Task.id, '/widgets/generic/tasklist/process.aspx', {submitOnBlur: true, okControl:false, cancelControl:false,callback:function(form,value){return "val=" + encodeURIComponent(value);}});
      TaskItem.InPlaceCollectionEditor = new Ajax.InPlaceCollectionEditor(div_Priority.id, '/widgets/generic/tasklist/process.aspx', {collection: ['low','med','high'],submitOnChange: true, okControl:false, cancelControl:false,callback:function(form,value){return "val=" + encodeURIComponent(value);}});
      
      this.Items.push(TaskItem);
   }
}